package com.barbeque.mall.util;

import com.barbeque.mall.bean.config.MarketSystem;
import com.barbeque.mall.bean.config.MarketSystemExample;
import com.barbeque.mall.mapper.MarketSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;


import java.lang.reflect.Field;

/**
 * @author Tezel
 * @since 2022/07/18 14:43
 */

public class ConfigServiceReflectionUtils {




    public static  <T> void updateConfig( T resource ,MarketSystemMapper systemMapper  ) {

        Class<?> aClass = resource.getClass();

        Field[] declaredFields = aClass.getDeclaredFields();

        for (Field declaredField : declaredFields) {

            declaredField.setAccessible(true);

            MarketSystem marketSystem = new MarketSystem();

            marketSystem.setKeyName(declaredField.getName());

            try {
                marketSystem.setKeyValue((String) declaredField.get(resource));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            criteria.andKeyNameLike(marketSystem.getKeyName());

            systemMapper.updateByExampleSelective(marketSystem, marketSystemExample);

        }
    }
}
